package leetcodev1.树;

import leetcodev1.链表.Solution;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

public class LeetCode99 {

    //这个思路不对，解不出来
    //关键是交换值，而不是真正交换节点
    List<TreeNode> needSwapNode = new ArrayList<>(2);

    public void recoverTree(TreeNode root) {
        dfs(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
        if (needSwapNode.size() == 2) {
            swapTreeNode(needSwapNode.get(0), needSwapNode.get(1));
        }
    }

    private void dfs(TreeNode node, int lower, int upper) {
        if (needSwapNode.size() == 2) {
            return;
        }

        if (node == null) {
            return;
        }

        if (node.val <= lower || node.val >= upper) {
            needSwapNode.add(node);
            dfs(node.left, lower, upper);
            dfs(node.left, lower, upper);
        } else {
            dfs(node.left, lower, node.val);
            dfs(node.left, node.val, upper);
        }
    }


    private void swapTreeNode(TreeNode node1, TreeNode node2) {
        TreeNode leftTmp = node1.left;
        TreeNode rightTmp = node1.right;
        node1.left = node2.left;
        node1.right = node2.right;
        node2.left = leftTmp;
        node2.right = rightTmp;
    }
}

//二叉搜索树中序遍历是有序的
//1.显式中序遍历
//2.隐式中序遍历
class Answer99 {
    public void recoverTree(TreeNode root) {
        List<Integer> nums = new ArrayList<Integer>();//中序遍历结果 基本有序
        inorder(root, nums);
        int[] swapped = findTwoSwapped(nums);//需要交换的节点
        recover(root, 2, swapped[0], swapped[1]);//执行交换
    }

    public void inorder(TreeNode root, List<Integer> nums) {
        if (root == null) {
            return;
        }
        inorder(root.left, nums);
        nums.add(root.val);
        inorder(root.right, nums);
    }

    public int[] findTwoSwapped(List<Integer> nums) {
        int n = nums.size();
        int index1 = -1, index2 = -1;
        for (int i = 0; i < n - 1; ++i) {
            if (nums.get(i + 1) < nums.get(i)) {
                index2 = i + 1;
                if (index1 == -1) {
                    index1 = i;
                } else {
                    break;
                }
            }
        }
        int x = nums.get(index1), y = nums.get(index2);
        return new int[]{x, y};
    }

    //交换值 但是不交换节点
    public void recover(TreeNode root, int count, int x, int y) {
        if (root != null) {
            if (root.val == x || root.val == y) {
                root.val = root.val == x ? y : x;
                if (--count == 0) {
                    return;
                }
            }
            recover(root.right, count, x, y);
            recover(root.left, count, x, y);
        }
    }

}


//1 4 3 2
//需要换的4和2
//x只需要更新一次 4
//因此需要不断更新y 3 2
class Solution98 {
    public void recoverTree(TreeNode root) {
        Deque<TreeNode> stack = new ArrayDeque<TreeNode>();
        TreeNode x = null, y = null, pred = null;

        while (!stack.isEmpty() || root != null) {
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            if (pred != null && root.val < pred.val) {
                y = root;
                if (x == null) {
                    x = pred;
                } else {
                    break;
                }
            }
            pred = root;
            root = root.right;
        }

        swap(x, y);
    }

    public void swap(TreeNode x, TreeNode y) {
        int tmp = x.val;
        x.val = y.val;
        y.val = tmp;
    }
}
